//
//  AutoRegisterCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 9/3/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

final class AutoCheckInfoCell: UITableViewCell {

    // MARK: - Outlets
    
    private lazy var rowStack: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.alignment = .fill
        stackView.distribution = .fillEqually
        stackView.spacing = 8
        return stackView
    }()
    
    private lazy var warnView: UIView = {
        
        let footerView = UIView()
        footerView.alpha = 0
        footerView.layer.cornerRadius = 15
        
        let titleTxt = UILabel()
        titleTxt.font = .systemFont(ofSize: 14)
        titleTxt.numberOfLines = 1
        titleTxt.sizeToFit()
        titleTxt.tag = 1
        
        let markIcon = UIImage(named: "warn")
        let markIconView = UIImageView(image: markIcon)
        markIconView.tag = 2
        
        let stackView = UIStackView(arrangedSubviews: [markIconView, titleTxt])
        stackView.axis = .horizontal
        stackView.alignment = .center
        stackView.distribution = .equalSpacing
        stackView.spacing = 16
        
        footerView.addSubview(stackView)
        
        markIconView.snp.makeConstraints { (make) in
            make.width.equalTo(16)
            make.height.equalTo(16)
        }
        stackView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.height.equalTo(20)
        }
        
        return footerView
    }()
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpEmpty () {
        _ = rowStack.arrangedSubviews.map {
            rowStack.removeArrangedSubview($0)
            $0.removeFromSuperview()
        }
        rowStack.removeFromSuperview()
        
        contentView.addSubview(warnView)
        warnView.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(48)
        }
    }
    
    private func addViews () {
        contentView.addSubview(rowStack)
        rowStack.snp.makeConstraints { (make) in
            make.top.leading.trailing.equalToSuperview().inset(10)
            make.bottom.equalToSuperview().offset(-16)
        }
    }
    
    private func setWarnText (isDTP: Bool) {
        let textLbl = (self.warnView.viewWithTag(1) as! UILabel)
        let iconView = self.warnView.viewWithTag(2) as! UIImageView
        warnView.alpha = 1
        warnView.backgroundColor = .lightBlueColor
        textLbl.textColor = .blueColor
        textLbl.text = isDTP ? "Данные о ДТП не найдены" : "История перерегистрации не найдена"
        iconView.image = UIImage(named: "warn")!.sd_tintedImage(with: .blueColor)
    }
}

extension AutoCheckInfoCell: ConfigurableCell {
    
    func configure(data object: AutoCheckViewModel.AutoCheckInfoItem?, index: Int) {
        if let _object = object {
            _ = rowStack.arrangedSubviews.map {
                rowStack.removeArrangedSubview($0)
                $0.removeFromSuperview()
            }
            
            if _object.isEmpty {
                self.setUpEmpty()
                self.setWarnText(isDTP: _object.isDTP)
            } else {
                for (i, key) in _object.data.keys.enumerated() {
                    let itemView = AutoCheckInfoField(placeholder: key)
                    itemView.tag = i
                    rowStack.addArrangedSubview(itemView)
                }
            }
        }
    }

    func returnData() -> AutoCheckViewModel.AutoCheckInfoItem? {
        return nil
    }
}



final class AutoCheckInfoField: UIView {
    private lazy var hintLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = .systemFont(ofSize: 14)
        lbl.textColor = .greyColor
        lbl.textAlignment = .left
        lbl.numberOfLines = 1
        lbl.sizeToFit()
        return lbl
    }()
    private lazy var valueLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = .systemFont(ofSize: 14)
        lbl.textColor = .darkBlueColor
        lbl.text = "*******"
        lbl.numberOfLines = 1
        lbl.sizeToFit()
        return lbl
    }()
    
    init (placeholder: String) {
        super.init(frame: .zero)
        self.hintLbl.text = placeholder
        addSubview(hintLbl)
        addSubview(valueLbl)
        
        hintLbl.snp.makeConstraints { (make) in
            make.top.leading.equalToSuperview()
        }
        valueLbl.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.top.equalTo(hintLbl.snp.bottom).offset(8)
            make.leading.bottom.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
